package com.mzba.place.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.mzba.place.entity.RegisterEntity;

/**
 * 
 * @author 06peng
 *
 */
public class UserTable {

	public String TABLE_NAME = "user_db";
	public static final String _id = "_id";
	public static final String authentication_token = "authentication_token";
	public static final String comments_count = "comments_count";
	public static final String created_at = "created_at";
	public static final String email = "email";
	public static final String id = "id";
	public static final String reports_count = "reports_count";
	public static final String stars_count = "stars_count";
	public static final String updated_at = "updated_at";
	public static final String username = "username";
	public static final String uuid = "uuid";
	public static final String avatar_url = "avatar_url";
	public static final String telphone = "telphone";
	
	private DBManagerImpl db = null;
	
	public UserTable(Context context) {
		if (db == null) {
			db = DBManager.get(context);
		}
		if (!db.isTableExits(db.getConnection(), TABLE_NAME)) {
			createTable();
		}
	}
	
	
	public void createTable() {
		String createTableSql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME
				+ "(_id integer primary key autoincrement, " 
				+ authentication_token + " VARCHAR,"  
				+ comments_count + "  INTEGER," 
				+ created_at + " VARCHAR,"
				+ email + " VARCHAR,"
				+ id + " INTEGER,"
				+ reports_count + " INTEGER,"
				+ stars_count + " INTEGER,"
				+ updated_at + " VARCHAR,"
				+ username + " VARCHAR,"
				+ uuid + " VARCHAR,"
				+ telphone + " VARCHAR,"
				+ avatar_url + " VARCHAR)";
		db.creatTable(db.getConnection(), createTableSql, TABLE_NAME);
		
	}
	
	public void save(RegisterEntity entry) {
		try {
			db.delete(db.getConnection(), TABLE_NAME, null, null);
			ContentValues values = new ContentValues();
			values.put(authentication_token, entry.getAuthentication_token());
			values.put(comments_count, entry.getComments_count());
			values.put(created_at, entry.getCreated_at());
			values.put(email, entry.getEmail());
			values.put(id, entry.getId());
			values.put(reports_count, entry.getReports_count());
			values.put(stars_count, entry.getStars_count());
			values.put(updated_at, entry.getUpdated_at());
			values.put(username, entry.getUsername());
			values.put(uuid, entry.getUuid());
			values.put(avatar_url, entry.getAvatar_url());
			values.put(telphone, entry.getTelphone());
			db.save(db.getConnection(), TABLE_NAME, values);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void update(RegisterEntity entry) {
		try {
			ContentValues values = new ContentValues();
			values.put(authentication_token, entry.getAuthentication_token());
			values.put(comments_count, entry.getComments_count());
			values.put(created_at, entry.getCreated_at());
			values.put(email, entry.getEmail());
			values.put(id, entry.getId());
			values.put(reports_count, entry.getReports_count());
			values.put(stars_count, entry.getStars_count());
			values.put(updated_at, entry.getUpdated_at());
			values.put(username, entry.getUsername());
			values.put(uuid, entry.getUuid());
			values.put(avatar_url, entry.getAvatar_url());
			values.put(telphone, entry.getTelphone());
			db.update(db.getConnection(), TABLE_NAME, values, null, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public RegisterEntity get() {
		RegisterEntity entry = null;
		Cursor cursor = db.find(db.getConnection(), "select * from " + TABLE_NAME, null);
		if (cursor != null && cursor.moveToFirst()) {
			entry = new RegisterEntity();
			entry.setAuthentication_token(cursor.getString(cursor.getColumnIndex(authentication_token)));
			entry.setAvatar_url(cursor.getString(cursor.getColumnIndex(avatar_url)));
			entry.setComments_count(cursor.getInt(cursor.getColumnIndex(comments_count)));
			entry.setCreated_at(cursor.getString(cursor.getColumnIndex(created_at)));
			entry.setEmail(cursor.getString(cursor.getColumnIndex(email)));
			entry.setId(cursor.getInt(cursor.getColumnIndex(id)));
			entry.setReports_count(cursor.getInt(cursor.getColumnIndex(reports_count)));
			entry.setStars_count(cursor.getInt(cursor.getColumnIndex(stars_count)));
			entry.setUpdated_at(cursor.getString(cursor.getColumnIndex(updated_at)));
			entry.setUsername(cursor.getString(cursor.getColumnIndex(username)));
			entry.setUuid(cursor.getString(cursor.getColumnIndex(uuid)));
			entry.setTelphone(cursor.getString(cursor.getColumnIndex(telphone)));
		}
		return entry;
	}
	
	public void clearTable() {
		db.delete(db.getConnection(), TABLE_NAME, null, null);
	}
}
